package com.biz.primus.account.config;

import com.biz.primus.account.exception.CasConfigExceptionType;
import com.biz.primus.base.exception.ExceptionType;
import com.biz.primus.base.exception.utils.AssertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * Created by younger on 17/11/3.
 * CAS的配置参数
 */
@Configuration
@ComponentScan("com.biz.primus.account")
@EnableFeignClients(basePackages = "com.biz.primus.account.feign")
public class CasConfig {

    @Autowired
    private Environment environment;

    /**
     * 当前应用的类型
     */
    public String getApplicationType(){

        String currentApplicationType = environment.getProperty("primus.application.type");
        AssertUtils.hasText(currentApplicationType, CasConfigExceptionType.MISSING_CONFIG, "未定义当前应用的类型: primus.application.type");
        return currentApplicationType;
    }

    /**
     * CasServer 服务器url地址
     */
    public String getCasServerUrl(){

        String casServerHost = environment.getProperty("primus.cas.server.host");
        AssertUtils.hasText(casServerHost, CasConfigExceptionType.MISSING_CONFIG, "未定义cas认证地址: primus.cas.server.host");
        return casServerHost;
    }

    /**
     * CasServer 服务器登录url地址
     */
    public String getCasServerLoginUrl(){
        return String.format("%s/login", getCasServerUrl());
    }

    /**
     * 退出url地址
     */
    public String getCasServerLogoutUrl() {
        return String.format("%s/logout?service=%s", getCasServerUrl(), getAppServerUrl());
    }

    /**
     * 应用地址
     */
    public String getAppServerUrl() {

        String currentApplicationHost = environment.getProperty("primus.application.host");
        AssertUtils.hasText(currentApplicationHost, CasConfigExceptionType.MISSING_CONFIG, "未定义当前应用的地址: primus.application.host");
        return currentApplicationHost;
    }

    /**
     * 应用登陆地址
     */
    public String getAppLoginUrl() {
        return String.format("/welcome");
    }

    /**
     * 应用退出地址
     */
    public String getAppLogoutUrl() {
        return String.format("/logout", getAppServerUrl());
    }
}
